// Description: Trigger to activate new or updated products loaded via the API
// Author(s): S.Gare & R.Clark
// Copyright: BrightGen Ltd (http://www.brightgen.com)
// Date: 12/03/2008
trigger Product_BeforeInsertUpdate on Product2 (before insert, before update) {
// Trigger to automatically set the Active flag to true 
// where the records come from SAP.

// NB: This trigger is not required for the SAP Product integration tool

    // Loop Through Inserted Products and Set IsActive on those that have been populated from SAP
    //   If you have a logical delete field you could check it here
    //   before enabling a product
    for(Product2 InsertedProducts: trigger.new){
        
        // If SAPProductStatus is Active then Set IsActive to TRUE
        if (InsertedProducts.ProductType__c == 'SAPProduct') {
            InsertedProducts.IsActive=True;
        }else {
            InsertedProducts.IsActive=False;
        }
        
        InsertedProducts.IsActive=True;
        
        // Map the SAP Product Code to SF Product Code
        // Requires custom SAP Product Code field
        InsertedProducts.ProductCode = InsertedProducts.SAPProductCode__c;
    }
 }